package com.xxxx.seckill.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 消息发送者（生产者）
 */
@Service
@Slf4j
public class MQSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(Object message) {
        log.info("发送消息：{}", message);
        rabbitTemplate.convertAndSend("queue", message); // 使用的是AMQP默认的交换机
    }

    public void sendFanout(Object message) {
        log.info("发送消息：{}", message);
        rabbitTemplate.convertAndSend("fanoutExchange", "", message); // 发送到广播交换机
    }

    public void sendDirect01(Object message) {
        log.info("发送red消息：{}", message);
        rabbitTemplate.convertAndSend("directExchange", "queue.red", message); // 发送到广播交换机
    }

    public void sendDirect02(Object message) {
        log.info("发送green消息：{}", message);
        rabbitTemplate.convertAndSend("directExchange", "queue.green", message); // 发送到广播交换机
    }

    public void sendTopic01(Object message) {
        log.info("发送消息（被QUEUE01接收）：{}", message);
        rabbitTemplate.convertAndSend("topicExchange", "queue.red.message", message);
    }

    public void sendTopic02(Object message) {
        log.info("发送消息（被两个QUEUE接收）：{}", message);
        rabbitTemplate.convertAndSend("topicExchange", "message.queue.green.abc", message);
    }

    public void sendHeaders01(String message) {
        log.info("发送消息（被两个QUEUE接收）：{}", message);
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setHeader("colour", "red");
        messageProperties.setHeader("speed", "fast");
        Message message0 = new Message(message.getBytes(), messageProperties);
        rabbitTemplate.convertAndSend("headersExchange", "", message0);
    }

    public void sendHeaders02(String message) {
        log.info("发送消息（被QUEUE01接收）：{}", message);
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setHeader("colour", "red");
        messageProperties.setHeader("speed", "normal");
        Message message0 = new Message(message.getBytes(), messageProperties);
        rabbitTemplate.convertAndSend("headersExchange", "", message0);
    }

    /**
     * 发送秒杀信息
     *
     * @param message:秒杀信息
     */
    public void sendSeckillMessage(String message) {
        log.info("发送信息：{}", message);
        rabbitTemplate.convertAndSend("seckillExchange", "seckill.message", message);
    }
}
